let MS = require("./ms.js")
const request = require("./request.js")
const Error = require('./error')
const storage = require("./storage.js")
const {
    isString,
    isUndefined,
    isNumber,
    isBoolean,
    isArray,
    isObject
} = require('./dataType')

const user = class user {
    constructor() {

    }

    login(code) {
        if (!isString(code)) {
            throw new Error(415)
        }
        let data = {code: code}
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.USER.LOGIN.url, MS._config.parameters.USER.LOGIN.method, data).then(res => {
                storage.save('msUser', res.data)
                resolve(res)
            }).catch(err => {
                reject(err)
            })
        })
    }

    update(res) {
        var currUser = this.current();
        let data = {
            sessionKey: currUser.sessionKey,
            signature: res.signature,
            rawData: res.rawData,
            encryptedData: res.encryptedData,
            iv: res.iv
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.USER.UPDATE_USER.url, MS._config.parameters.USER.UPDATE_USER.method, data).then(res => {
                storage.save('msUser', res.data)
                resolve(res)
            }).catch(err => {
                reject(err)
            })
        })
    }

    refreshCurrent() {
        var currUser = this.current();
        let params = {
            id : currUser.id
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.USER.GET_WX_USER.url,
                MS._config.parameters.USER.GET_WX_USER.method, params).then(res => {
                if(res.success) {
                    storage.save('msUser', res.data)
                    resolve(res.data)
                } else {
                    reject(res.message)
                }
            }).catch(err => {
                reject(err);
            })
        })
    }

    current() {
        const data = storage.fetch('msUser')
        if (data != undefined && data != null) {
            return typeof data === 'object' ? data : JSON.parse(data)
        } else {
            return null;
        }
    }

    employee() {
        const data = storage.fetch('employee')
        if (data != undefined && data != null) {
            return typeof data === 'object' ? data : JSON.parse(data)
        } else {
            return null;
        }
    }

    bindPhone(phone) {
        var currUser = this.current();
        if (!Object(currUser)) {
            throw new Error(415, "当前用户不存在")
        }
        if (isUndefined(phone) && !isString(phone)) {
            throw new Error(415, "手机号不能为空")
        }
        let data = {
            userId: currUser.id,
            phoneNumber: phone
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.USER.BIND_PHONE.url, MS._config.parameters.USER.BIND_PHONE.method, data).then(res => {
                if (res.success) {
                    resolve(res.data)
                    storage.save('msUser', res.data)
                } else {
                    reject(res.message)
                }
            }).catch(err => {
                reject(err)
            })
        })
    }

    getPhoneNumber(e) {
        var currUser = this.current();
        if (!Object(currUser)) {
            throw new Error(415, "当前用户不存在")
        }
        let data = {
            userId: currUser.id,
            encryptedData: e.detail.encryptedData,
            iv: e.detail.iv
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.USER.GET_PHONE_NUMBER.url, MS._config.parameters.USER.GET_PHONE_NUMBER.method, data).then(res => {
                if (res.success) {
                    resolve(res.data)
                } else {
                    reject(res.message)
                }
            }).catch(err => {
                reject(err)
            })
        })
    }

    findUnPayShopBags() {
        var currUser = this.current();
        if (!Object(currUser)) {
            throw new Error(415, "当前用户不存在")
        }
        let data = {userId: currUser.id}
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.USER.FIND_UN_PAY_SHOP_BAGS.url,
                MS._config.parameters.USER.FIND_UN_PAY_SHOP_BAGS.method, data).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    getUnPayShopBagNum() {
        var currUser = this.current();
        if (!Object(currUser)) {
            throw new Error(415, "当前用户不存在")
        }
        let data = {userId: currUser.id}
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.USER.GET_UN_PAY_SHOP_BAG_NUM.url,
                MS._config.parameters.USER.GET_UN_PAY_SHOP_BAG_NUM.method, data).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    joinShopBag(params) {
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.USER.JOIN_SHOP_BAG.url,
                MS._config.parameters.USER.JOIN_SHOP_BAG.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    plusShopBagGoods(shopBagId) {
        let data = {shopBagId: shopBagId}
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.USER.PLUS_SHOP_BAG_GOODS.url,
                MS._config.parameters.USER.PLUS_SHOP_BAG_GOODS.method, data).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    minusShopBagGoods(shopBagId) {
        let data = {shopBagId: shopBagId}
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.USER.MINUS_SHOP_BAG_GOODS.url,
                MS._config.parameters.USER.MINUS_SHOP_BAG_GOODS.method, data).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    removeShopBagGoods(shopBagId) {
        let data = {shopBagId: shopBagId}
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.USER.REMOVE_SHOP_BAG_GOODS.url,
                MS._config.parameters.USER.REMOVE_SHOP_BAG_GOODS.method, data).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    findSysNotice(pageNum, pageSize) {
        var pn = pageNum == undefined || pageNum == null ? 1 : pageNum;
        var ps = pageSize == undefined || pageSize == null ? 10 : pageSize;
        var currUser = this.current();
        if (!Object(currUser)) {
            throw new Error(415, "当前用户不存在")
        }
        let data = {
            userId: currUser.id,
            pageNum: pn,
            pageSize: ps
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.USER.FIND_SYS_NOTICE.url,
                MS._config.parameters.USER.FIND_SYS_NOTICE.method, data).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    getNoLookSysNoticeNum() {
        var currUser = this.current();
        if (!Object(currUser)) {
            throw new Error(415, "当前用户不存在")
        }
        let data = {
            userId: currUser.id
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.USER.GET_NO_LOOK_SYS_NOTICE_NUM.url,
                MS._config.parameters.USER.GET_NO_LOOK_SYS_NOTICE_NUM.method, data).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    setSysNoticeLook() {
        var currUser = this.current();
        if (!Object(currUser)) {
            throw new Error(415, "当前用户不存在")
        }
        let data = {
            userId: currUser.id
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.USER.SET_SYS_NOTICE_LOOK.url,
                MS._config.parameters.USER.SET_SYS_NOTICE_LOOK.method, data).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    findUserBill(options) {
        var currUser = this.current();
        if (!Object(currUser)) {
            throw new Error(415, "当前用户不存在")
        }
        let data = {
            userId: currUser.id,
            pageNum: 1,
            pageSize: 10
        }
        if (!isUndefined(options.pageNum) && isNumber(options.pageNum)) {
            data.pageNum = options.pageNum;
        }
        if (!isUndefined(options.pageSize) && isNumber(options.pageSize)) {
            data.pageSize = options.pageSize;
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.USER.FIND_USER_BILL.url,
                MS._config.parameters.USER.FIND_USER_BILL.method, data).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    feedback(remark) {
        var currUser = this.current();
        if (!Object(currUser)) {
            throw new Error(415, "当前用户不存在")
        }
        if (isUndefined(remark)) {
            throw new Error(415, "反馈内容为空")
        }
        let data = {
            userId: currUser.id,
            remark: remark
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.FEEDBACK.FEEDBACK_CREATE.url,
                MS._config.parameters.FEEDBACK.FEEDBACK_CREATE.method, data).then(res => {
                if (res.success) {
                    resolve(res.data)
                } else {
                    reject(res.message)
                }
            }).catch(err => {
                reject(err)
            })
        })
    }

    findFeedback(options) {
        var currUser = this.current();
        if (!Object(currUser)) {
            throw new Error(415, "当前用户不存在")
        }
        var data = {
            userId: currUser.id,
            pageNum: 1,
            pageSize: 10
        }
        if (!isUndefined(options.pageNum) && isNumber(options.pageNum)) {
            data.pageNum = options.pageNum
        }
        if (!isUndefined(options.pageSize) && isNumber(options.pageSize)) {
            data.pageSize = options.pageSize
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.FEEDBACK.FIND_FEEDBACK.url,
                MS._config.parameters.FEEDBACK.FIND_FEEDBACK.method, data).then(res => {
                if (res.success) {
                    resolve(res.data)
                } else {
                    reject(res.message)
                }
            }).catch(err => {
                reject(err)
            })
        })
    }

    query(options) {
        let params = {
            pageNum: 1,
            pageSize: 10
        }
        if (!isUndefined(options.nickName) && isString(options.nickName)) {
            params.nickName = options.nickName;
        }
        if (!isUndefined(options.isAgent) && isBoolean(options.isAgent)) {
            params.isAgent = options.isAgent;
        }
        if (!isUndefined(options.gender) && isNumber(options.gender)) {
            params.gender = options.gender;
        }
        if (!isUndefined(options.pageNum) && isNumber(options.pageNum)) {
            params.pageNum = options.pageNum
        }
        if (!isUndefined(options.pageSize) && isNumber(options.pageSize)) {
            params.pageSize = options.pageSize
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.USER.FIND_WX_USER.url,
                MS._config.parameters.USER.FIND_WX_USER.method, params).then(res => {
                if (res.success) {
                    resolve(res.data)
                } else {
                    reject(res.message)
                }
            }).catch(err => {
                reject(err)
            })
        })
    }

    getUser(id) {
        let params = {
            id : id
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.USER.GET_WX_USER.url,
                MS._config.parameters.USER.GET_WX_USER.method, params).then(res => {
                    if(res.success) {
                        resolve(res.data)
                    } else {
                        reject(res.message)
                    }
            }).catch(err => {
                reject(err);
            })
        })
    }

    openIM(id) {
        let params = {
            id: this.current() ? this.current().id : null
        }
        if(!isUndefined(id) && isString(id)) {
            params.id = id;
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.USER.OPEN_IM.url,
                MS._config.parameters.USER.OPEN_IM.method, params).then(res => {
                    if(res.success) {
                        resolve(res.data)
                        storage.save('msUser', res.data)
                    } else {
                        reject(res.message)
                    }
            }).catch(err => {
                reject(err)
            })
        })
    }

    closeIM(id) {
        let params = {
            id: this.current() ? this.current().id : null
        }
        if(!isUndefined(id) && isString(id)) {
            params.id = id;
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.USER.CLOSE_IM.url,
                MS._config.parameters.USER.CLOSE_IM.method, params).then(res => {
                    if(res.success) {
                        resolve(res.data)
                        storage.save('msUser', res.data)
                    } else {
                        reject(res.message)
                    }
            }).catch(err => {
                reject(err)
            })
        })
    }

    scan(options) {
        let params = {};
        if(!isUndefined(options) && isObject(options)) {
            if(!isUndefined(options.userId) && isString(options.userId)) {
                params.userId = options.userId;
            }
            if(!isUndefined(options.userName) && isString(options.userName)) {
                params.userName = options.userName;
            }
            if(!isUndefined(options.targetId) && isString(options.targetId)) {
                params.targetId = options.targetId;
            }
            if(!isUndefined(options.targetName) && isString(options.targetName)) {
                params.targetName = options.targetName;
            }
            if(!isUndefined(options.targetType) && isNumber(options.targetType)) {
                params.targetType = options.targetType;
            }
            if(!isUndefined(options.longitude) && isNumber(options.longitude)) {
                params.longitude = options.longitude;
            }
            if(!isUndefined(options.latitude) && isNumber(options.latitude)) {
                params.latitude = options.latitude;
            }
            return new Promise((resolve, reject) => {
                request(MS._config.parameters.USER.SCAN.url,
                    MS._config.parameters.USER.SCAN.method, params).then(res => {
                    if(res.success) {
                        resolve(res.data)
                    } else {
                        reject(res.message)
                    }
                }).catch(err => {
                    reject(err)
                })
            })
        }
    }

    setAgent(userId) {
        let params = {
            id: this.current() ? this.current().id : null
        }
        if(!isUndefined(userId) && isString(userId)) {
            params.id = userId;
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.USER.SET_AGENT.url,
                MS._config.parameters.USER.SET_AGENT.method, params).then(res => {
                if(res.success) {
                    resolve(res.data)
                } else {
                    reject(res.message)
                }
            }).catch(err => {
                reject(err)
            })
        })
    }

    cancelAgent(userId) {
        let params = {
            id: this.current() ? this.current().id : null
        }
        if(!isUndefined(userId) && isString(userId)) {
            params.id = userId;
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.USER.CANCEL_AGENT.url,
                MS._config.parameters.USER.CANCEL_AGENT.method, params).then(res => {
                if(res.success) {
                    resolve(res.data)
                } else {
                    reject(res.message)
                }
            }).catch(err => {
                reject(err)
            })
        })
    }

    updateCustomData(options) {
        let params = {
            userId: this.current() ? this.current().id : null
        }
        if(!isUndefined(options) && isObject(options)) {
            if(!isUndefined(options.userId) && isString(options.userId)) {
                params.userId = options.userId;
            }
            if(!isUndefined(options.customName) && isString(options.customName)) {
                params.customName = options.customName;
            }
            if(!isUndefined(options.customAvatarUrl) && isString(options.customAvatarUrl)) {
                params.customAvatarUrl = options.customAvatarUrl;
            }
            if(!isUndefined(options.synopsis) && isString(options.synopsis)) {
                params.synopsis = options.synopsis;
            }
            if(!isUndefined(options.birthday) && isString(options.birthday)) {
                params.birthday = options.birthday
            }
            return new Promise((resolve, reject) => {
                request(MS._config.parameters.USER.UPDATE_CUSTOM_DATA.url,
                    MS._config.parameters.USER.UPDATE_CUSTOM_DATA.method, params).then(res => {
                        if(res.success) {
                            resolve(res.data)
                            storage.save('msUser', res.data)
                        } else {
                            reject(res.message)
                        }
                }).catch(err => {
                    reject(err)
                })
            })
        }
    }

    fidnMediaUsers() {
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.USER.FIND_WX_USER_BY_MEDIA.url,
                MS._config.parameters.USER.FIND_WX_USER_BY_MEDIA.method, {}).then(res =>{
                    if(res.success) {
                        resolve(res.data)
                    } else {
                        reject(res.message)
                    }
            }).catch(err => {
                reject(err)
            })
        })
    }
}
module.exports = user